using CustomerDomain;
using EasyNetQ;
using MF.Bus;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CustomerService.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

   
        private readonly ILogger<WeatherForecastController> _logger;

        private static int count3 = 0;
        private static int count4 = 0;

        private readonly IBus _bus;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IBus bus)
        {
            _logger = logger;
            _bus = bus;
        }

        [SubscribeAttribute("order.created","test3")]
        public void TestSubscribe(BusMessage textMessage)
        {
            lock(this)
            {
                count3++;
            }
            var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            var msg = string.Format("time:{0},事件:{1},队列:{2},消息:{3}", timestamp, "order.created", "test3", textMessage.JsonData);
            Console.WriteLine(msg);
            throw new Exception("aaa");
            //return "hello";
        }


        [SubscribeAttribute("order.created", "test4")]
        public void HandleTextMessage1(BusMessage textMessage)
        {
            lock (this)
            {
                count4++;
            }
            var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            var msg = string.Format("time:{0},事件:{1},队列:{2},消息:{3}", timestamp, "order.created", "test4", textMessage.JsonData);
            Console.WriteLine(msg);
        }

 
        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpGet("GetNum")]
        //[Route("GetNum")]
        public String GetNum()
        {
            string msg = string.Format("本次消费情况:队列3消费{0}个,队列4消费{1}个,队列5消费{2}个", count3, count4, DemoDomain.count5);
            Console.WriteLine(msg);
            //清空
            lock(this)
            {
                count3 = 0;
                count4 = 0;
                DemoDomain.count5 = 0;
            }
            return msg;
        }
    }
}
